let curr = 1;
let pagenum = 0;
let query_item_name = "";
let query_item_state = "";

let app = new Vue({
	el:".app",
	data:{
		list:[],
		total:10,
		curr:1,
		size:10,
		idzhi:[],
		abab:[],
		
		ckAll:false,
		// con:'',
		
		add_show:false,
		mod_show:false,
		
		// 添加
		add_item_name: "",
		add_item_price: "",
		add_item_format: "",
		add_item_total: "",
		add_item_inventory: "",
		
		// 修改
		mod_item_name: "",
		mod_item_price: "",
		mod_item_format: "",
		mod_item_total: "",
		mod_item_inventory: "",
		
		inp:"",
		sta:"",
	},
	created: function(){
		this.getItem();
	},
	
	computed:{
		pagenum:function(){
			this.curr = 1;
			return Math.ceil(this.total/this.size);
		}
	},
	
	methods:{
		getItem:function(){
			axios.get('http://chonghekj.com/crud/index.php/Home/Index/getItemList',{
				params:{
					pageSize:this.size,
					pageNumber:this.curr,
					item_name:this.inp,
					item_state:this.sta,
				}
			})
			.then((res) => {
				this.list = res.data.rows;
				this.total = res.data.total;
			})
		},
		getCurr:function(curr){
			this.curr = curr;
			this.getItem();
		},
		changepage:function(){
			this.curr = 1;
			this.getItem();
		},
		addItem:function(){
			this.add_show = true;
		},
		add_Submit:function(){
			axios.post("http://chonghekj.com/crud/index.php/Home/Index/addItem",Qs.stringify({
				item_name:this.add_item_name,
				item_price:this.add_item_price,
				item_format:this.add_item_format,
				item_total:this.add_item_total,
				item_inventory:this.add_item_inventory,
				
			}))
			.then((res) => {
				console.log(this.add_item_name);
				this.curr = 1;
				this.getItem();
				this.add_show = false;
				// this.curr = 1;
				this.add_item_name = "";
				this.add_item_price = "";
				this.add_item_format = "";
				this.add_item_total = "";
				this.add_item_inventory = "";
				
			})
		},
		add_cancel:function(){
			this.add_show = false;
			this.curr = 1;
			this.getItem();
		},
		change_Opt:function(item){
			if(this.idzhi.length == this.list.length){
				this.ckAll = true;
			}else{
				this.ckAll = false;
			}
		},
		
		modifyItem:function(){
			if(this.idzhi.length == 1){
				this.mod_show = true;
				for(let i=0; i<this.list.length; i++){
					if(this.list[i].item_id == this.idzhi){
						this.abab.push(this.list[i]);
					}
				}
				this.mod_item_id = this.abab[0].item_id;
				this.mod_item_name = this.abab[0].item_name;
				// console.log(this.mod_item_name);
				this.mod_item_price = this.abab[0].item_price;
				this.mod_item_format = this.abab[0].item_format;
				this.mod_item_total = this.abab[0].item_total;
				this.mod_item_inventory = this.abab[0].item_inventory;
			}else if(this.idzhi.length > 1){
				
				this.mod_show = false;
				alert("多选无法修改，请重新选择！！！");
				// this.con = confirm('多选无法修改，请重新选择！！！')
				// if(this.con){
				// 	this.getItem();
				// }else{
				// 	this.getItem();
				// }
				
				
			}else{
				this.mod_show = false;
				alert("不选择，你怎么修改！！！")
			}
		},
		
		mod_cancel:function(){
			this.mod_show = false;
			this.abab=[];
			this.idzhi=[];
			// this.mod_item_id = "";
			// this.mod_item_name = "";
			// this.mod_item_price = "";
			// this.mod_item_format = "";
			// this.mod_item_total = "";
			// this.mod_item_inventory = "";
		},
		mod_Submit:function(){
			axios.post("http://chonghekj.com/crud/index.php/Home/Index/editItem",Qs.stringify({
					item_id:this.mod_item_id,
					item_name:this.mod_item_name,
					item_format:this.mod_item_format,
					item_total:this.mod_item_total,
					item_inventory:this.mod_item_inventory,
			}))
			.then((res) =>{
				console.log(111)
				this.getItem();
				this.mod_show = false;
				this.abab=[];
				this.idzhi=[];
				// this.mod_item_id = "";
				// this.mod_item_name = "";
				// this.mod_item_price = "";
				// this.mod_item_format = "";
				// this.mod_item_total = "";
				// this.mod_item_inventory = "";
				
			})
		},
		deleteItem:function(){
			if(this.idzhi.length >= 1){
				let con = confirm("是否删除当前"+this.idzhi.length+"条数据");
				if(con){
					axios.post('http://chonghekj.com/crud/index.php/Home/Index/deleteItem',Qs.stringify({
						ids:this.idzhi,
					}))
					.then((res) =>{
						this.getItem();
						this.idzhi = [];
					})
				}
			}		
		},
		query:function(){
			this.getItem();	
			this.inp = "";
		},
		changeAll:function(){
			if(this.ckAll){
				this.idzhi = this.list.map(function(val){
					return val.item_id;
				})
			}else{
				this.idzhi = [];
			}
		},
	}
})